<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING); 
//ini_set('display_errors', '1');

class Message extends a_base {

	public function index()
	{
		$where = "guestbookid` !=\"\" ";
		$search = $this->input->post();
		if(!empty($search)){
			foreach ($search as $key => $value) {
				if($value == ''){
					unset($search[$key]);
					continue;
				}else{
					if($key == 'nickname' || $key == 'bookcontent'){
						//$this->db->like('title', 'match', 'both');
						$where .= " AND `$key` LIKE '%$value%'";
					}elseif($key == 'isreply' || $key == 'isclose'){
						$where .= " AND `$key` = '$value'";
					}
				}
			}
		}
		$page = isset($_GET['page'])?$_GET['page']:1;//当前页
		$cntArr = $this->m_public->getRowInfo('guestbook',array(),'count(1) as cnt');
		$arr = array (
			'page' => $page,//当前页
		    'page_size' => 10,     //分页总大小
		    'total' => $cntArr['cnt']//总记录数
		);
		$start_page = ($page-1)*10;
		$data['list'] = $this->m_public->getPageAllInfo('guestbook',$where,10,$start_page);
		//print_R($data['list']);
		$data['pagination'] = new pagination($arr);
		$this->load->view('message/index',$data);
	}

	public function add_message($guestbookid='')
	{
		if($guestbookid){
			$data['info'] = $this->m_public->getRowInfo('guestbook',array('guestbookid'=>$guestbookid));
			$data['admin'] = $this->m_public->getAllInfo('admin',array(),'adminid,adminname');
		}else{
			$data['info'] = '';
		}
		$this->load->view('message/add_message',$data);
	}

    public function view_message($guestbookid='')
    {
        if($guestbookid){
            $data['info'] = $this->m_public->getRowInfo('guestbook',array('guestbookid'=>$guestbookid));
            $data['admin'] = $this->m_public->getAllInfo('admin',array(),'adminid,adminname');
        }else{
            $data['info'] = '';
        }
        $this->load->view('message/view_message',$data);
    }

	public function delete_info($url,$parmykey,$table,$ids)
	{
		if(!$ids){
			redirect("message/$url");
		}
		$idArr = explode('.', $ids);
		foreach($idArr as $k=>$v){
			$res = $this->m_public->delInfo($table,array($parmykey=>$v));
		}
		redirect("message/$url");
	}

	public function edit_close($url,$parmykey,$table,$val,$ids)
	{
		if(!$ids){
			redirect(b_url("message/$url"));
		}
		$idArr = explode('.', $ids);
		foreach($idArr as $k=>$v){
			$data['isclose'] = $val;
			$this->m_public->editInfo(array($parmykey=>$v),$data,$table);
		}
		redirect("message/$url");
	}

	public function message_action($guestbookid='')
	{
		$info = $this->input->post();
		//print_r($info);
		if($guestbookid){
			if($info['replycontent']){
					$info['isreply'] = 1;
					$info['replytime'] = date('Y-m-d H:i:s');
				}else{
					$info['isreply'] = 0;
					$info['replytime'] = NULL;
				}
			$this->m_public->editInfo(array('guestbookid'=>$guestbookid),$info,'guestbook');
		}else{
			if(!empty($info)){
				$info['addtime'] = date('Y-m-d H:i:s');
				$info['ipaddress'] = $this->m_public->get_client_ip();
				$info['adminid'] = $this->session->userdata['user_id'];
				if($info['replycontent']){
					$info['isreply'] = 1;
					$info['replytime'] = date('Y-m-d H:i:s');
				}else{
					$info['isreply'] = 0;
					$info['replytime'] = NULL;
				}
				$this->m_public->addRowInfo('guestbook',$info);
			}
		}
		redirect('message');
	}

	public function feedback()
	{
		$where = "feedbackid` !=\"\" ";
		$search = $this->input->post();
		if(!empty($search)){
			foreach ($search as $key => $value) {
				if($value == ''){
					unset($search[$key]);
					continue;
				}else{
					if(($key == 'typeid' && $value == '-1') || ($key == 'isdeal' && $value == '-1')){
						unset($search[$key]);
						continue;
					}
					if($key == 'title' || $key == 'feedbackcontent'){
						//$this->db->like('title', 'match', 'both');
						$where .= " AND `$key` LIKE '%$value%'";
					}elseif($key == 'typeid' || $key == 'isdeal' || $key == 'ipaddress'){
						$where .= " AND `$key` = '$value'";
					}elseif($key == 'addtime1' ){
						$where .= " AND `addtime` >= '$value' ";
					}elseif($key == 'addtime2' ){
						$where .= " AND `addtime` <= '$value' ";
					}
				}
			}
		}
		$page = isset($_GET['page'])?$_GET['page']:1;//当前页
		$cntArr = $this->m_public->getRowInfo('feedback',$where,'count(1) as cnt');
		$arr = array (
			'page' => $page,//当前页
		    'page_size' => 10,     //分页总大小
		    'total' => $cntArr['cnt']//总记录数
		);
		$start_page = ($page-1)*10;
		$data['list'] = $this->m_public->getPageAllInfo('feedback',$where,10,$start_page);
		//print_R($data['list']);
		$data['pagination'] = new pagination($arr);
		$this->load->view('message/feedback',$data);
	}

	public function feedback_action($feedbackid='')
	{
		$info = $this->input->post();
		if(!empty($info)){
			$dealArr = $this->m_public->getRowInfo('feedback',array('feedbackid'=>$feedbackid),'dealmeno');
			if(empty($dealArr['dealmeno'])){
				$info['isdeal'] = 1;
				$info['dealmeno'] = "处理结果:".$info['dealmeno']."<br>处理人:".$this->session->userdata['user_name']."<br>处理时间:".date('Y-m-d H:i:s')."<br>=======================================<br>";
			}else{
				$info['dealmeno'] = $dealArr['dealmeno']."处理结果:".$info['dealmeno']."<br>处理人:".$this->session->userdata['user_name']."<br>处理时间:".date('Y-m-d H:i:s')."<br>=======================================<br>";
			}
			$this->m_public->editInfo(array('feedbackid'=>$feedbackid),$info,'feedback');
		}
		redirect('message/feedback');
	}

	public function add_feedback($feedbackid='')
	{
		if($feedbackid){
			$data['info'] = $this->m_public->getRowInfo('feedback',array('feedbackid'=>$feedbackid));
			//$data['admin'] = $this->m_public->getAllInfo('admin',array(),'adminid,adminname');
		}else{
			$data['info'] = '';
		}
		$this->load->view('message/add_feedback',$data);
	}

}
